// pages/index/searchresult/searchresult.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderNum: '',
    processId: '',
    type: '',
    count: 0,
    serialNumberList: [],
    selectList: [],
    code: '',
  },
  //获取订单号
  async getStartProductionList() {
    var data = await global.$api.post({
      url: 'api/startProduction/getStartProductionList',
      data: {
        orderNum: this.data.orderNum,
        processId: this.data.processId,
        userId: wx.getStorageSync('currentUserId'),
        type: this.data.type
      }
    })
    if (data.code == 200 || data.code == 201) {
      wx.stopPullDownRefresh()
      console.log(data.data)
      data.data.forEach(res => {
        res.select = false
      })
      this.setData({
        serialNumberList: data.data,
        code: data.code,
      })
    } else {
      wx.navigateBack({
        delta: 1,
      })
      setTimeout(() => {
        wx.showToast({
          title: data.message,
          icon: 'none'
        })
      }, 500)
    }
  },

  //选择编号
  selectWordNum(e) {
    console.log(e)
    var temp_str = this.data.serialNumberList[e.currentTarget.dataset.select].select;
    var temp = 'serialNumberList[' + e.currentTarget.dataset.select + '].select';
    let select = this.data.serialNumberList[e.currentTarget.dataset.select].id
    let selects = this.data.selectList
    if (temp_str) {
      selects.forEach((res, index) => {
        if (res == select) {
          selects.splice(index, 1)
        }
      })
      this.setData({
        [temp]: false,
        selectList: selects
      });
    } else {
      selects.push(select)
      this.setData({
        [temp]: true,
        selectList: selects
      });
    }
  },

  //保存
  save() {
    if (this.data.selectList.length == 0) {
      wx.showToast({
        title: '请选择您的订单',
        icon: 'none'
      })
    } else {
      this.startProduction()
    }
  },
  //取消
  handleCancel() {
    wx.showModal({
      title: '警告', //提示的标题
      content: '您确认要执行该操作吗', //提示的内容
      success: (res) => {
        if (res.confirm) {
          this.canceling()
        } else if (res.cancel) {

        }
      }
    })
  },
  async canceling() {
    var data = await global.$api.post({
      url: 'api/skirt/canceling',
      data: {
        handOrderNum: this.data.orderNum
      }
    })
    if (data.code == 200) {
      console.log(data.data)
      wx.showToast({
        title: '取消成功',
        icon: 'none'
      })
      this.setData({
        selectList: []
      })
      this.getStartProductionList()
    } else {
      wx.showToast({
        title: data.message,
        icon: 'none'
      })
    }
  },



  async startProduction() {
    var data = await global.$api.post({
      url: 'api/startProduction/startProduction',
      data: {
        productionOrderSonIds: this.data.selectList.toString(),
        processId: this.data.processId,
        userId: wx.getStorageSync('currentUserId'),
        type: this.data.type
      }
    })
    if (data.code == 200) {
      console.log(data.data)
      wx.showToast({
        title: '保存成功',
        icon: 'none'
      })
      this.setData({
        selectList: []
      })
      this.getStartProductionList()
    } else {
      wx.showToast({
        title: data.message,
        icon: 'none'
      })
    }
  },

  //扫描
  scan() {
    wx.redirectTo({
      url: `../scan/scan?type=1&&id=${this.data.processId}&&process=1`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderNum: options.orderNum,
      processId: options.processId,
      type: options.from
    })
    this.getStartProductionList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      serialNumberList: [],
      selectList: []
    })
    this.getStartProductionList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})